import collections


class Solution(object):
    def findLUSlength(self, strs):
        """
        :type strs: List[str]
        :rtype: int
        """

        def judge(s1, s2):
            n1 = len(s1)
            n2 = len(s2)
            left = 0
            right = 0
            while left < n1 and right < n2:
                if s1[left] == s2[right]:
                    left += 1
                    right += 1
                else:
                    right += 1
            if left == n1:
                return True
            else:
                return False

        d_wait = collections.Counter(strs)
        d_wait_len = collections.defaultdict(set)
        max_len = 0
        for key in d_wait:
            max_len = max(max_len, len(key))
            if d_wait[key] == 1:
                d_wait_len[len(key)].add(key)
        if d_wait_len[max_len]:
            return max_len
        d_target = set()
        for key in d_wait:
            if len(key) == max_len:
                d_target.add(key)
        l_list = list(d_wait_len.keys())
        l_list.sort(reverse=True)
        for length in l_list:
            for word in d_wait_len[length]:
                flag = True
                for p in d_target:
                    if judge(word, p):
                        flag = False
                        break
                if flag:
                    return length
        return -1


data = Solution()
strs = ["aba", "cdc", "eae"]
print(data.findLUSlength(strs))
strs = ["aaa", "aaa", "aa",'aa','a','c']
print(data.findLUSlength(strs))
